package com.wangyk.oauth2.config;

import com.wangyk.oauth2.pojo.MyUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configuration.GlobalAuthenticationConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class WebSecurityConfiguration extends GlobalAuthenticationConfigurerAdapter
{

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    public void init(AuthenticationManagerBuilder auth)
        throws Exception
    {
        //TODO:use md5
        auth.userDetailsService(userDetailsService)
            .passwordEncoder(new PasswordEncoder()
            {
                @Override
                public String encode(CharSequence charSequence)
                {
                    return charSequence.toString();
                }

                @Override
                public boolean matches(CharSequence charSequence, String s)
                {
                    return charSequence.toString().equals(s);
                }
            });
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth)
        throws Exception
    {
        //inMemoryAuthentication 从内存中获取
        /*
        auth.inMemoryAuthentication()
            .passwordEncoder(new BCryptPasswordEncoder())
            .withUser("test")
            .password(new BCryptPasswordEncoder().encode("123456"))
            .roles("USER");
        */
        //使用数据库中的用户名和密码
        //注入userDetailsService的实现类
    }
}
